package com.shujia.core

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo17SortBy {
  case class StuSort(id: String, name: String, age: String, gender: String, clazz: String)

  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf()
    conf.setAppName("Demo17SortBy")
    conf.setMaster("local")

    val sc: SparkContext = new SparkContext(conf)

    val stuSortRDD: RDD[StuSort] = sc.textFile("Spark/data/students.txt")
      .map(line => {
        val splits: Array[String] = line.split(",")
        StuSort(splits(0), splits(1), splits(2), splits(3), splits(4))
      })

    // 指定按照年龄进行排序
    /**
     * sortBy 转换算子 指定排序的字段
     * ascending：默认为true表示升序，如果置为false表示降序排列
     */
    stuSortRDD
      .sortBy(s => s.age, ascending = false)
      .foreach(println)


  }


}
